package com.like.fragment;

import org.xutils.x;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.like.entity.M;

public class BaseFragment extends Fragment {

    protected View mRootView;
    private boolean injected = false;

    protected boolean mPrepared = false;
    protected boolean mHasLoadOnce = false;
    protected M m;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (mRootView == null) {
            injected = true;
            mRootView = x.view().inject(this, inflater, container);
            onOnlyOnceInit();
            mPrepared = true;
            lazyLoad();
        }
        m = new M(this);
        onCreateView();
        return mRootView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!injected) {
            x.view().inject(this, this.getView());
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (mPrepared)
                onVisiable();
            lazyLoad();
        } else {
            if (mPrepared)
                onInvisiable();
        }
    }

    private void lazyLoad() {
        if (!mPrepared)
            return;
        lazyLoadEveryTime();
        if (!mHasLoadOnce) {
            lazyLoadOnce();
            mHasLoadOnce = true;
        }
    }

    protected void onOnlyOnceInit() {
    }

    protected void onCreateView() {
    }

    protected void lazyLoadOnce() {
    }

    protected void lazyLoadEveryTime() {
    }

    protected void onVisiable() {
    }

    protected void onInvisiable() {
    }

}
